Intersect get_march_point(vec3 p, Plane plane) {
    return Intersect(plane.normal, dot((p-plane.normal*plane.len), plane.normal), plane.material);
}

Intersect get_march_point(vec3 p, Sphere sphere) {
    vec3 cp = p-sphere.center;
    return Intersect(normalize(cp), length(cp)-sphere.radius, sphere.material);
}

Intersect march(Ray ray) {
    int step = 0;
    for(float len = 0.0;len < MAX_LEN && step < MAX_STEP;step++){
        vec3 p = ray.origin+ray.dir*len;

        Intersect best = Intersect(vec3(0), MAX_LEN, Material(vec3(0), 0));
        for(int i =0;i<PLANE_COUNT;i++){
            Intersect tmp = get_march_point(p, planes[i]);
            if (tmp.len<best.len){
                best = tmp;
            }
        }

        for(int i=0;i<SPHERE_COUNT;i++) {
            Intersect tmp = get_march_point(p, spheres[i]);
            if(tmp.len<best.len){
                best = tmp;
            }
        }

        if(best.len < MIN_DISTANCE){
            return Intersect(best.normal, len, best.material);
        }
        len += best.len;
    }
    return miss;
}

float fresnel(float r0, float cos) {
    return r0 + (1 - r0) * pow((1 - cos), 5);
}
